package com.iims.web.controller.system;

import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.domain.entity.SysRole;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.common.utils.SecurityUtils;
import com.iims.common.utils.poi.ExcelUtil;
import com.iims.system.domain.SysGroup;
import com.iims.system.service.ISysGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 分组信息操作处理
 *
 * @author fanpei
 */
@Api(tags = "SYS-组管理")
@RestController
@RequestMapping("/system/group")
public class SysGroupController extends BaseController {
    @Autowired
    private ISysGroupService groupService;

    /**
     * 获取分组列表
     */
    @ApiOperation("获取分组列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query"),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query"),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式", name = "isAsc", paramType = "query", allowableValues = "asc,desc"),
            @ApiImplicitParam(value = "分组名称", name = "groupName", paramType = "query"),
            @ApiImplicitParam(value = "分组编码", name = "groupCode", paramType = "query"),
            @ApiImplicitParam(value = "状态:值来源于字典sys_normal_disable", name = "status", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore SysGroup group) {
        startPage();
        List<SysGroup> list = groupService.selectGroupList(group);
        return getDataTable(list);
    }

    @Log(title = "分组管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:group:export')")
    @GetMapping("/export")
    public AjaxResult export(SysGroup group) {
        List<SysGroup> list = groupService.selectGroupList(group);
        ExcelUtil<SysGroup> util = new ExcelUtil<SysGroup>(SysGroup.class);
        return util.exportExcel(list, "分组数据");
    }

    /**
     * 根据分组编号获取详细信息
     */
    @ApiOperation("根据分组编号获取详细信息")
    @ApiImplicitParam(name = "groupId", value = "分组编号", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable Long groupId) {
        return AjaxResult.success(groupService.selectGroupById(groupId));
    }

    /**
     * 新增分组
     */
    @ApiOperation("新增分组")
    @PreAuthorize("@ss.hasPermi('system:group:add')")
    @Log(title = "分组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysGroup group) {
        if (UserConstants.NOT_UNIQUE.equals(groupService.checkGroupNameUnique(group))) {
            return AjaxResult.error("新增分组'" + group.getGroupName() + "'失败，分组名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(groupService.checkGroupCodeUnique(group))) {
            return AjaxResult.error("新增分组'" + group.getGroupName() + "'失败，分组编码已存在");
        }
        group.setCreateBy(SecurityUtils.getUsername());
        return toAjax(groupService.insertGroup(group));
    }

    /**
     * 修改分组
     */
    @ApiOperation("修改分组")
    @PreAuthorize("@ss.hasPermi('system:group:edit')")
    @Log(title = "分组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysGroup group) {
        if (UserConstants.NOT_UNIQUE.equals(groupService.checkGroupNameUnique(group))) {
            return AjaxResult.error("修改分组'" + group.getGroupName() + "'失败，分组名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(groupService.checkGroupCodeUnique(group))) {
            return AjaxResult.error("修改分组'" + group.getGroupName() + "'失败，分组编码已存在");
        }
        group.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(groupService.updateGroup(group));
    }

    /**
     * 删除分组
     */
    @ApiOperation("删除分组")
    @PreAuthorize("@ss.hasPermi('system:group:remove')")
    @Log(title = "分组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(groupService.deleteGroupByIds(groupIds));
    }

    /**
     * 获取分组选择框列表
     */
    @ApiOperation("获取分组选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysGroup> groups = groupService.selectGroupAll();
        return AjaxResult.success(groups);
    }

    @ApiOperation("获取指定用户已关联分组列表")
    @ApiImplicitParam(name = "userId", value = "用户编号", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('system:group:group_list')")
    @GetMapping("/list/group/{userId}")
    public AjaxResult inCludeGroupList(@PathVariable Long userId) {
        List<SysGroup> groups = groupService.selectGroupListByUserId(userId);
        return AjaxResult.success(groups);
    }

    @ApiOperation("获取指定用户未关联分组列表[排除节点]")
    @ApiImplicitParam(name = "userId", value = "用户编号", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('system:group:group_list')")
    @GetMapping("/list/group/exclude/{userId}")
    public AjaxResult excludeGroupList(@PathVariable Long userId) {
        List<SysGroup> groups = groupService.selectExcludeGroupListByUserId(userId);
        return AjaxResult.success(groups);
    }

    @ApiOperation("获取指定分组已关联角色列表")
    @ApiImplicitParam(name = "groupId", value = "分组编号", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('system:group:role_list')")
    @GetMapping("/list/role/{groupId}")
    public AjaxResult inCludeRoleList(@PathVariable Long groupId) {
        List<SysRole> roles = groupService.selectRoleListByGroupId(groupId);
        return AjaxResult.success(roles);
    }

    @ApiOperation("获取指定分组未关联角色列表[排除节点]")
    @ApiImplicitParam(name = "groupId", value = "分组编号", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('system:group:role_list')")
    @GetMapping("/list/role/exclude/{groupId}")
    public AjaxResult excludeRoleList(@PathVariable Long groupId) {
        List<SysRole> roles = groupService.selectExcludeRoleListByGroupId(groupId);
        return AjaxResult.success(roles);
    }
}
